package com.ilop.sthome.ui.activity.mine;

import android.text.TextUtils;

import com.aliyun.iot.aep.sdk.login.LoginBusiness;
import com.aliyun.iot.aep.sdk.login.data.UserInfo;
import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.SpUtil;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.mvp.contract.mine.SetUpContract;
import com.ilop.sthome.mvp.present.mine.SetUpPresenter;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivitySetUpBinding;

/**
 * @author skygge
 * @date 2020-01-07.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：系统设置界面
 */
public class SetUpActivity extends ContainLoginActivity<SetUpPresenter, ActivitySetUpBinding> implements SetUpContract.IView {

    private boolean isPhone;
    private String userName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_up;
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new SetUpPresenter(mContext);
    }

    @Override
    protected void initialize() {
        super.initialize();
        if (LoginBusiness.isLogin()) {
            UserInfo userInfo = LoginBusiness.getUserInfo();
            if (userInfo != null) {
                if (!TextUtils.isEmpty(userInfo.userPhone)) {
                    isPhone = true;
                    userName = userInfo.userPhone;
                } else if (!TextUtils.isEmpty(userInfo.userEmail)) {
                    isPhone = false;
                    userName = userInfo.userEmail;
                }
            }
        }else {
            finish();
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivSetUpBack.setOnClickListener(view -> finish());

        mDBind.setUpPwd.setOnClickListener(v -> {
            if(isPhone){
                SpUtil.putString(mContext, CommonId.CHANGE_PASSWORD, userName);
                mPresent.modifyPhonePassword();
            }else {
                SpUtil.putString(mContext, CommonId.CHANGE_PASSWORD, userName);
                mPresent.modifyEmailPassword();
            }
        });
        mDBind.setUpLogOff.setOnClickListener(v -> skipAnotherActivity(CancellationActivity.class));
    }

    @Override
    public void showToastMsg(String msg) {
        showToast(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!TextUtils.isEmpty(SpUtil.getString(mContext, CommonId.CHANGE_PASSWORD))){
            SpUtil.putString(mContext, CommonId.CHANGE_PASSWORD, "");
        }
    }
}
